package com.mail.anteater.core;

import org.apache.log4j.Logger;

import com.mail.anteater.config.Config;
import com.mail.anteater.config.ConfigValues;
import com.mail.anteater.facade.AntMailService;
import com.mail.anteater.hibernate.Cronjob;
import com.mail.anteater.hibernate.SessionFactoryUtil;
import com.mail.anteater.util.Func;
import com.mail.anteater.bean.Category;
import com.mail.anteater.bean.Filter;
import com.mail.anteater.bean.MailResponse;



public class MailMain {

	final static Logger logger = Logger.getLogger(MailMain.class);
	
	private static Config config = null;
	private static SessionFactoryUtil hibernateSession = null; 
	private static ConfigValues configValues = null;
	private static Cronjob cronjob = null;
	private AntMailService mailService;
	
	public static void main(String [] args){
		MailMain main = new MailMain();
		
		/*ArrayList<String> receivers = new ArrayList<String>();
		receivers.add("eclipse");
		receivers.add("haseo");
		ArrayList<String> cc = new ArrayList<String>();
		cc.add("ning");
		String contents = "Hello this is the first test mail.";
		String instruction = "there are no instruction available yet...";
		Priority priority = Priority.NORMAL;
		Type type = Type.PERSONAL;
		main.getMailService().sendMail("hwa", receivers, cc, contents, instruction, priority, type);*/
		Filter filter = new Filter();
		filter.setPage(1);
		filter.setRecordPerPage(10);
		filter.setCc("ning");
		MailResponse response = main.getMailService().retrieveMail("seng", filter, Category.INBOX);
		Func.println(response.toString());

		/*MailResponse response = main.getMailService().readMail("ning", 3, Category.Inbox);
		Func.println(response.toString());*/
		/*Filter filter = new Filter();
		ArrayList<Long> ids = new ArrayList<Long>();
		ids.add(Long.valueOf(3));
		filter.setIds(ids);
		main.getMailService().deleteMail("ning", filter, Category.Inbox);*/
		/*String a = null;
		Func.println(a == null ? "dd" : "bb");*/
	}
	
	public MailMain() {
		// start all servers
		startServers();
		mailService = new AntMailService();
	}
	
	private void startServers(){
		startConfigurationServer();
		startSettingServer();
		startHibernateServer();
		startCronjobServer();
	}
	
	public void destroy(){
		stopConfigurationServer();
		stopSettingServer();
		stopHibernateServer();
		stopCronjobServer();
	}
	
	private void startConfigurationServer(){
		config = new Config();
	}
	private void stopConfigurationServer(){
		if(config != null) config.stop();
	}
	private void startSettingServer(){
		configValues = new ConfigValues();
	}
	private void stopSettingServer(){
		configValues = null;
	}
	private void startHibernateServer(){
		hibernateSession = new SessionFactoryUtil(); 
	}
	private void stopHibernateServer(){
		SessionFactoryUtil.close();
		hibernateSession = null;
	}
	private void startCronjobServer(){
		cronjob = new Cronjob();
	}
	private void stopCronjobServer(){
		if(cronjob != null){
			cronjob.stop();
			cronjob = null;
		}
	}

	public static Config getConfig() {
		return config;
	}

	public static void setConfig(Config config) {
		MailMain.config = config;
	}

	public static SessionFactoryUtil getHibernateSession() {
		return hibernateSession;
	}

	public static void setHibernateSession(SessionFactoryUtil hibernateSession) {
		MailMain.hibernateSession = hibernateSession;
	}

	public ConfigValues getConfigValues() {
		return configValues;
	}

	public AntMailService getMailService() {
		return mailService;
	}

	public void setMailService(AntMailService mailService) {
		this.mailService = mailService;
	}
}
